package com.lightkits.wms.domain.changelabeldocument.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.database.dao.ChangeLabelDocumentMapper;
import com.lightkits.wms.database.model.changelabeldocument.ChangeLabelDocument;
import com.lightkits.wms.domain.changelabeldocument.ChangeLabelDocumentService;
import com.lightkits.wms.dto.changelabeldocument.GetChangeLabelDocumentRequest;
import com.lightkits.wms.dto.changelabeldocument.GetChangeLabelDocumentResponse;
import com.lightkits.wms.dto.changelabeldocument.SynchronousChangeLabelDocumentDataResponse;
import com.lightkits.wms.dto.changelabeldocument.model.ChangeLabelDocumentDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author lingyun.zhong@hand-china.com
 * @date 2019/7/19 16:29
 **/
@Service
public class ChangeLabelDocumentServiceImpl implements ChangeLabelDocumentService {

    private ChangeLabelDocumentMapper documentMapper;

    public ChangeLabelDocumentServiceImpl(ChangeLabelDocumentMapper documentMapper) {
        this.documentMapper = documentMapper;
    }

    @Override
    public GetChangeLabelDocumentResponse getChangeLabelDocument(GetChangeLabelDocumentRequest request) {
        boolean isPageHelper = request.getPageNum() != null && request.getPageSize() != null;
        Page<ChangeLabelDocument> documentPage = null;
        if (isPageHelper) {
            documentPage = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        }
        List<ChangeLabelDocument> labelDocumentList = documentMapper.getChangeLabelDocument(
                request.getOrganizationName(),
                request.getWorkOrderNumber(),
                request.getExecutionStatus(),
                request.getOrderSubTypeCode());
        GetChangeLabelDocumentResponse response = new GetChangeLabelDocumentResponse();
        if (isPageHelper) {
            response.setPageCount((int) documentPage.getTotal());
        }
        if (ObjectUtil.isNullOrEmpty(labelDocumentList)) {
            response.setDtoList(new ArrayList<>());
            return response;
        }
        response.setDtoList(this.builderChangeLabelDocumentDTO(labelDocumentList));
        return response;
    }

    @Override
    public SynchronousChangeLabelDocumentDataResponse synchronousChangeLabelDocumentData() {
        // todo 手动刷新调用逻辑代码
        SynchronousChangeLabelDocumentDataResponse response = new SynchronousChangeLabelDocumentDataResponse();
        return response;
    }

    private List<ChangeLabelDocumentDTO> builderChangeLabelDocumentDTO(List<ChangeLabelDocument> labelDocuments) {
        List<ChangeLabelDocumentDTO> list = new ArrayList<>();
        for (ChangeLabelDocument document : labelDocuments) {
            list.add(ChangeLabelDocumentDTO.builder()
                    .organizationName(document.getOrganizationName())
                    .workOrderNumber(document.getWorkOrderNumber())
                    .orderStatusCode(document.getOrderStatusCode())
                    .executionStatus(document.getExecutionStatus())
                    .orderSubTypeCode(document.getOrderSubTypeCode())
                    .productItemNum(document.getProductItemNum())
                    .productQuantity(document.getProductQuantity())
                    .productUom(document.getProductUom())
                    .componentItemNum(document.getComponentItemNum())
                    .componentQuantity(document.getComponentQuantity())
                    .componentUom(document.getComponentUom())
                    .orderCreatorName(document.getOrderCreatorName())
                    // todo 字段调整
                    .comments(document.getComments())
                    .build());
        }
        return list;
    }
}
